﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using com.lover.astd.common.config;
using com.lover.astd.common.model;
using com.lover.astd.common;
using com.lover.astd.common.logic;

namespace com.lover.astd.game.ui.server.impl.troop
{
    /// <summary>
    /// 英雄洗属性服务
    /// </summary>
    public class HeroWashServer : LogicServer
    {
        public HeroWashServer(MainForm frm) { _mainForm = frm; ServerName = ConfigStrings.S_HeroWash; ServerReadableName = ConfigStrings.SR_HeroWash; }
        public override void saveSettings()
        {
            GameConfig conf = getConfig();
            MainForm frm = _mainForm;
            conf.setConfig(ServerName, ConfigStrings.enabled, frm.chk_hero_wash_enable.Checked.ToString());
            object obj = frm.combo_hero_wash.SelectedValue;
            if (obj == null) conf.setConfig(ServerName, ConfigStrings.wash_hero, "");
            else conf.setConfig(ServerName, ConfigStrings.wash_hero, frm.combo_hero_wash.SelectedValue.ToString());
            string sb = "";
            if (frm.chk_hero_wash_tong.Checked) sb += "1";
            else sb += "0";
            if (frm.chk_hero_wash_yong.Checked) sb += "1";
            else sb += "0";
            if (frm.chk_hero_wash_zhi.Checked) sb += "1";
            else sb += "0";
            conf.setConfig(ServerName, ConfigStrings.wash_attrib, sb);
        }
        public override void renderSettings()
        {
            if (_mainForm.combo_hero_wash.DataSource == null)
            {
                BindingSource src = new BindingSource();
                src.DataSource = getUser().Heroes;
                _mainForm.combo_hero_wash.DataSource = src;
            }

            Dictionary<string, string> conf = getConfig(ServerName);
            MainForm frm = _mainForm;
            frm.chk_hero_wash_enable.Checked = conf.ContainsKey(ConfigStrings.enabled) && conf[ConfigStrings.enabled].ToLower().Equals("true");
            if (conf.ContainsKey(ConfigStrings.wash_hero) && conf[ConfigStrings.wash_hero] != "")
            {
                int wash_hero = 0;
                if (Int32.TryParse(conf[ConfigStrings.wash_hero], out wash_hero))
                {
                    frm.combo_hero_wash.SelectedValue = wash_hero;
                    string wash_attrib = conf[ConfigStrings.wash_attrib];
                    if (wash_attrib.Length >= 3)
                    {
                        frm.chk_hero_wash_tong.Checked = wash_attrib[0] == '1';
                        frm.chk_hero_wash_yong.Checked = wash_attrib[1] == '1';
                        frm.chk_hero_wash_zhi.Checked = wash_attrib[2] == '1';
                    }
                }
            }
            refreshData();
        }
        private void refreshData()
        {
            _mainForm.combo_hero_wash.Refresh();
        }
        public override void loadDefaultSettings()
        {
            GameConfig conf = getConfig();
            conf.setConfig(ServerName, ConfigStrings.enabled, "true"); // 洗将开关
            conf.setConfig(ServerName, ConfigStrings.wash_hero, ""); // 洗谁的属性, 武将的id
            conf.setConfig(ServerName, ConfigStrings.wash_attrib, "000"); // 洗什么
            renderSettings();
        }
    }
}
